#include "MainView.h"

MainView::MainView()
{

    root = QFileInfo(__FILE__).absolutePath();
    scene = new QGraphicsScene();
    bg = new BackGround(scene);
    guy = new Guy(scene);
    guy->setPos(this->width()*0.5,this->height()*0.5);


    cts = new ComeToScene(scene);
    guydeath = new GuyDeath(scene);
    rotatetime = 0;
    guydeathtime = 0;

    contralor = new TotalControl(scene);


//    smallEnemy1 = new SmallEnemy(scene);
//    smallEnemy1->setPos(QPointF(600,600));
    //smallEnemy1->setVisible(true);

//    followSmallEnemy1 = new FollowSmallEnemy(scene);
//    followSmallEnemy1->setPos(QPointF(600,600));
//    followSmallEnemy1->setVisible(true);


    aim = QPixmap(root + "/res/crosshair.png");
    gameovertip = new QGraphicsPixmapItem(QPixmap(root+"/res/press_space.png"));
    gameovertip->setPos(200,600);
    gameovertip->setVisible(false);
    gamethrough = new QGraphicsPixmapItem(QPixmap(root + "/res/level_cleared.png"));
    gamethrough->setPos(200,600);
    gamethrough->setVisible(false);
    scene->addItem(gameovertip);
    scene->addItem(gamethrough);
    this->setCursor(QCursor(aim,-1,-1));



    this->setFixedSize(1600,900);
    this->setWindowTitle("Paperwar");
    this->setWindowIcon(QIcon(root + "/res/doom_guy.png"));
    scene->setSceneRect(0,0,this->width()-2,this->height()-2);
    this->setScene(scene);

    totalTimer = new QTimer(this);//建立总定时器
    connect(totalTimer,SIGNAL(timeout()),this,SLOT(TotalTimer()));

    CTSTimer = new QTimer(this); //角色出场动画定时器
    connect(CTSTimer,SIGNAL(timeout()),this,SLOT(ComeToSceneAniTimer()));

    guyDeathTimer = new QTimer(this); //角色死亡动画定时器
    connect(guyDeathTimer,SIGNAL(timeout()),this,SLOT(GuyDeathAniTimer()));

    oldguyDeathTimer = new QTimer(this); //老角色死亡动画定时器
    connect(oldguyDeathTimer,SIGNAL(timeout()),this,SLOT(oldGuyDeathAniTimer()));

    Keyx = 0.0f;
    Keyy = 0.0f;
    x=0.0f;
    y = 0.0f;
    rotate = 0.0;
    MoveSign = 0;
    index = 0;
    isPlaying = 0;
    ShouldCallEnemy = 950;

    setMouseTracking(true);

    shootsignal = 0;//不发射子弹

    //敌人碰撞线程
    thread = new QThread(this);

    allbulletlist = guy->bulletlist;

    bulletTouch = new BulletTouch(allbulletlist,contralor->SmallEnemyList,contralor->FollowEnemyList,contralor->BigEnemyList);//这里还缺
    bulletTouch->moveToThread(thread);//捆绑线程
    connect(this,&MainView::wStart,bulletTouch,&BulletTouch::run);
    thread->start();

    //人物碰撞线程
    thread2 = new QThread(this);
    manTouch = new ManTouchThread(guy,contralor->SmallEnemyList,contralor->FollowEnemyList,contralor->BigEnemyList,oldguylist);//缺少
    manTouch->moveToThread(thread2);
    connect(this,&MainView::meStart,manTouch,&ManTouchThread::run);
    thread2->start();

    guyComeToScenePos();
    CTSTimer->start(50);
    oldguyDeathTimer->start(100);

}

void MainView::guyComeToScenePos(){

    guydeathtime = 0;
    oldguylist.append(new OldGuy(scene));
    oldguyposlist.append(QList<QPointF*>());
    oldguyrotatelist.append(QList<double>());
    oldguyshootlist.append(QList<int>());


    for(int j=0;j<oldguylist.length()-1;j++)
        allbulletlist += oldguylist[j]->bulletlist;
    bulletTouch->SendBullet(allbulletlist);
    manTouch->SendOldGuy(oldguylist);

    Keyx = QRandomGenerator::global()->bounded(150,1450);
    Keyy = QRandomGenerator::global()->bounded(150,780);
    cts->setPos(Keyx,Keyy);
    guy->setVisible(true);
    guy->setPos(Keyx,Keyy);
}

void MainView::ComeToSceneAniTimer(){
    rotatetime++;
    //qDebug()<<rotatetime;


    if(rotatetime>=50){
        totalTimer->start(40);
        CTSTimer->stop();
        isPlaying = 1;
        MoveSign = 0;
        //guyDeathTimer->start(100);
        for(int i=0;i<oldguylist.size()-1;i++){
            oldguylist[i]->setVisible(true);
        }
    }
    else{
        cts->updateAnimation();
        guy->UpdateAni();
    }
}

void MainView::GuyDeathAniTimer(){
    guydeathtime++;
    qDebug()<<guydeathtime;
    if(guydeathtime>=6){
        guydeathtime = 0;
        guydeath->setVisible(false);
        guyDeathTimer->stop();
        gameovertip->setVisible(true);
    }
    else {
        guydeath->setVisible(true);
        guydeath->setPos(Keyx,Keyy);
        guydeath->updateDeathAnimation();
    }

}

void MainView::oldGuyDeathAniTimer(){
    for(int j=0;j<oldguylist.length();j++)
        oldguylist[j]->updateDeathAnimation();
}

void MainView::RestartGame(int t){
    if(t==1){
        oldguylist.clear();
        oldguyposlist.clear();
        oldguyrotatelist.clear();
        oldguyshootlist.clear();
    }
    else if(t==0){
        for(auto oldguy:oldguylist){
            oldguy->isdeath = 0;
        }
    }
    for(auto oldguy:oldguylist){
        oldguy->setVisible(false);
    }

    //totalTimer->stop();

    guy->isdeath = 0;
    guy->opacity = 0;
    guy->state_index1 = 0;
    guy->state_index2 = 0;
    guy->setVisible(true);
    gameovertip->setVisible(false);
    gamethrough->setVisible(false);
    bg->sliderx = 294;
    cts->rotatespeed = 10;
    cts->opacity = 1;
    rotatetime = 0;

    index = 0;
    guyComeToScenePos();
    CTSTimer->start(50);


}

void MainView::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Space&&isPlaying==0){
        if(bg->sliderx>=1313&&guy->isdeath==0)
            RestartGame(1); //通关
        else if(guy->isdeath==1)
            RestartGame(0); //没通关
    }
    if(isPlaying==0) return;
    if(event->key()==Qt::Key_A){
        MoveSign+=1;
    }else if(event->key()==Qt::Key_W){
        MoveSign+=2;
    }else if(event->key()==Qt::Key_S){
        MoveSign+=4;
    }else if(event->key()==Qt::Key_D){
        MoveSign+=6;
    }


}

void MainView::keyReleaseEvent(QKeyEvent *event)
{
    if(isPlaying==0) return;
    if(event->key()==Qt::Key_A){
        MoveSign-=1;
    }else if(event->key()==Qt::Key_W){
        MoveSign-=2;
    }else if(event->key()==Qt::Key_S){
        MoveSign-=4;
    }else if(event->key()==Qt::Key_D){
        MoveSign-=6;
    }
}

void MainView::mouseReleaseEvent(QMouseEvent *event)
{
    if(isPlaying==0) return;
    shootsignal = 0;
}

void MainView::mousePressEvent(QMouseEvent *event)
{
    //qDebug("1");
    if(isPlaying==0) return;
    shootsignal = 1;
}

void MainView::Shooting()
{
    if(shootsignal==1){
        guy->Shooting();//发射zidan
        //qDebug("shoot");
    }
}

void MainView::mouseMoveEvent(QMouseEvent *event)
{
    if(isPlaying==0) return;
    x=event->x();
    y=event->y();
    qline = new QLineF(x,y,Keyx,Keyy);
    rotate = 180-qline->angle();

}

void MainView::updateGuyPos()
{
    if(Keyx+guy->pix.height()>this->width()){
        Keyx = this->width()-guy->pix.height();
    }
    if(Keyy+guy->pix.width()>this->height()){
        Keyy = this->height()-guy->pix.height();
    }
    if(Keyx-guy->pix.height()<0){
        Keyx = guy->pix.height();
    }
    if(Keyy-guy->pix.height()<0){
        Keyy = guy->pix.height();
    }
    guy->setPos(QPointF(Keyx,Keyy));

}

void MainView::updateOldGuy(){
    if(oldguylist.size()<=1) return;
    for(int i=0;i<oldguylist.size()-1;i++){
        if(index < oldguyposlist[i].size() && oldguylist[i]->isdeath == 0){
            oldguylist[i]->setPos(*oldguyposlist[i][index]);
            oldguylist[i]->setState(oldguyrotatelist[i][index]);
            if (oldguyshootlist[i][index] == 1)
                oldguylist[i]->Shooting();
        }
    }
    index += 1;
}

void MainView::TotalTimer()
{
    if(bg->sliderx>=1313){  //判断是否通关
        isPlaying = 0;
        gamethrough->setVisible(true);
    }
    if(guy->isdeath==1&&isPlaying==1){
        ShouldCallEnemy=950;
        isPlaying = 0;
        guyDeathTimer->start(100);
        MoveSign = 0;
    }
    //if(isPlaying < 0 ) return;
    bg->updateslider();
    scene->advance();   
    qDebug()<<MoveSign;
    guy->setState(rotate);
    updateGuyPos();
    Shooting();

    if(MoveSign<0) MoveSign=0;
    if(MoveSign==1){
        Keyx-=8;
    }else if(MoveSign==2){
        Keyy-=8;
    }else if(MoveSign==3){
        Keyx-=6;
        Keyy-=6;
    }else if(MoveSign==4){
        Keyy+=8;
    }else if(MoveSign==5){
        Keyx-=6;
        Keyy+=6;
    }else if(MoveSign==6){
        Keyx+=8;
    }else if(MoveSign==8){
        Keyx+=6;
        Keyy-=6;
    }else if(MoveSign==10){
        Keyx+=6;
        Keyy+=6;
    }
    if(ShouldCallEnemy==1000){
        contralor->CreateSmallEnemy(0);

        contralor->CreateSmallEnemy(1);

        contralor->CreateFollowEnemy(0);
        contralor->CreateBigEnemy(0);
        ShouldCallEnemy =0;
    }
    ShouldCallEnemy++;

    contralor->EnemyFollow(Keyx,Keyy);

    emit wStart();
    emit meStart();

    //记录角色行为
    if(isPlaying){
        oldguyposlist.last().append(new QPointF(Keyx,Keyy));
        oldguyrotatelist.last().append(rotate);
        oldguyshootlist.last().append(shootsignal);
    }

    if(isPlaying==1)
        updateOldGuy();


}
